package com.hevery.web.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author: zhy
 * @description: 用一个对象来模拟一个消息队列
 * @date: created on 2017/10/15
 * @modified by:
 */
@Component
public class MockQueue {

    /**代表下单消息*/
    private String placeOrder;
    /**代表订单完成消息*/
    private String completeOrder;

    private Logger logger = LoggerFactory.getLogger(getClass());

    public String getPlaceOrder() {
        return placeOrder;
    }

    /**
     * 模拟下单，此处是应用2来处理的，所以放到一个单独的线程里面
     * @param placeOrder
     * @throws Exception
     */
    public void setPlaceOrder(String placeOrder) throws Exception{
        new Thread(() -> {
            logger.info("接到下单请求，"+placeOrder);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /**将completeOrder设置为placeOrder意味着下单完成*/
            this.completeOrder = placeOrder;
            logger.info("下单请求处理完毕，"+placeOrder);
        }).start();
    }

    public String getCompleteOrder() {
        return completeOrder;
    }

    public void setCompleteOrder(String completeOrder) {
        this.completeOrder = completeOrder;
    }
}
